package com.example.hello;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FirstListActivity extends ListActivity implements Runnable {
    private static final String TAG = "FirstListActivity";

    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*List<String> list1=new ArrayList<String>();
        for(int i=1;i<=10;i++){
            list1.add("item "+i);
        }
        ListAdapter adaper=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list1);
        setListAdapter(adaper);

         */
        Thread t = new Thread(this);
        t.start();
        handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                String rate = "";
                if (msg.what == 12) {
                    ArrayList<String> list1 = (ArrayList<String> ) msg.obj;
                    ListAdapter adaper=new ArrayAdapter<String>(FirstListActivity.this, android.R.layout.simple_list_item_1,list1);
                    setListAdapter(adaper);
                    super.handleMessage(msg);
                }
            }


        };
    }
    //-----------------------------------------子线程-------------------------------
    public void run() {
        Log.i(TAG, "run: ......");
        Document doc = null;
        try {
            doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "title:" + doc.title());
        Element table = doc.getElementsByTag("table").get(1);
        Log.i(TAG, "table=" + table);
        Elements trs = table.getElementsByTag("tr");
        List<String> list1=new ArrayList<String>();
        for (Element tr : trs) {
            Elements tds = tr.getElementsByTag("td");
            Log.i(TAG, "run:tds=" + tds);
            if (tds.size() > 0) {
                String str = tds.get(0).text();
                Log.i(TAG, "run: str=" + str);
                String val = tds.get(5).text();
                Log.i(TAG, "run: val=" + val);
                list1.add(str + "->" + val);
            }
            Message msg = handler.obtainMessage(12, list1);
            handler.sendMessage(msg);
        }
         /*
        try {
            doc = Jsoup.connect("http://www.usd-cny.com/icbc.htm").get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "title:" + doc.title());
        Element table = doc.getElementsByTag("table").first();
        Log.i(TAG, "table=" + table);
        Elements trs = table.getElementsByTag("tr");
        List<String> list1=new ArrayList<String>();
        for (Element tr : trs) {
            Elements tds = tr.getElementsByTag("td");
            Log.i(TAG, "run:tds=" + tds);
            if (tds.size() > 0) {
                String str = tds.get(0).text();
                Log.i(TAG, "run: str=" + str);
                String val = tds.get(3).text();
                Log.i(TAG, "run: val=" + val);
                list1.add(str + "->" + val);
            }
            Message msg = handler.obtainMessage(12, list1);
            handler.sendMessage(msg);

        }

         */
    }
}
